import { 
  query as queryUsers, 
  queryCurrent, 
  queryPermission, 
  queryPermissionFather, 
  queryUrl, 
  updatePermission, 
  addPermission ,
  deletePermission,
  queryRole,
  addRole,
  updateRole,
  deleteRole,
  addUser,
  deleteUser,
  updateUser
} from '@/services/user';
import { message } from 'antd';

export default {
  namespace: 'user',

  state: {
    list: [],
    currentUser: {},
    pagination:[],
    permission:[],
    role:[],
    father:[],
    url:[],
  },

  effects: {
    *fetch(_, { call, put }) {
      const response = yield call(queryUsers);
      if(response.status === 'SUCCESS'){
        yield put({
          type: 'list',
          payload: response.data,
        });
      }
    },
    *fetchPermission(_, {call, put}){
      const response = yield call(queryPermission);
      if(response.status === 'SUCCESS'){
        yield put({
          type: 'permission',
          payload: response.data,
        });
      }
    },
    *fetchRole(_, {call, put}){
      const response = yield call(queryRole);
      if(response.status === 'SUCCESS'){
        yield put({
          type: 'role',
          payload: response.data,
        });
      }
    },
    *fetchUpdatePermission({ payload }, {call, put}){
      const response = yield call(updatePermission, payload);
      if(response.status === 'SUCCESS'){
        yield put({
          type: 'updatePermission',
          payload: response.data,
        });
      }
    },
    *fetchDeletePermission({ payload }, {call, put}){
      const response = yield call(deletePermission, payload);
      if(response.status === 'SUCCESS'){
        yield put({
          type: 'deletePermission',
          payload: response.data,
        });
      }
    },
    *fetchAddPermission({ payload }, {call, put}){
      const response = yield call(addPermission, payload);
      if(response.status === 'SUCCESS'){
        yield put({
          type: 'addPermission',
          payload: response.data,
        });
      }
    },
    *fetchAddUser({ payload }, {call, put}){
      const response = yield call(addUser, payload);
      if(response.status === 'SUCCESS'){
        yield put({
          type: 'addUser',
          payload: response.data,
        });
      }
    },
    *fetchUpdateUser({ payload }, {call, put}){
      const response = yield call(updateUser, payload);
      if(response.status === 'SUCCESS'){
        yield put({
          type: 'updateUser',
          payload,
        });
      }
    },
    *fetchDeleteUser({ payload }, {call, put}){
      const response = yield call(deleteUser, payload);
      if(response.status === 'SUCCESS'){
        message.success('删除成功');
        yield put({
          type: 'deleteUser',
          payload: response.data,
        });
      }else{
        message.error('删除失败');
      }
    },
    *fetchDeleteRole({ payload }, {call, put}){
      const response = yield call(deleteRole, payload);
      if(response.status === 'SUCCESS'){
        message.success('删除成功');
        yield put({
          type: 'deleteRole',
          payload: response.data,
        });
      }else{
        message.error('删除失败');
      }
    },
    *fetchAddRole({ payload }, {call, put}){
      const response = yield call(addRole, payload);
      if(response.status === 'SUCCESS'){
        message.success('操作成功');
        yield put({
          type: 'addRole',
          payload: response.data,
        });
      }else{
        message.error('操作失败');
      }
    },
    *fetchUpdateRole({ payload }, {call, put}){
      const response = yield call(updateRole, payload);
      if(response.status === 'SUCCESS'){
        message.success('操作成功');
        yield put({
          type: 'updateRole',
          payload: response.data,
        });
      }else{
        message.error('操作失败');
      }
    },
    *fetchPermissionFather(_, {call, put}){
      const response = yield call(queryPermissionFather);
      if(response.status === 'SUCCESS'){
        yield put({
          type: 'permissionFather',
          payload: response.data,
        });
      }
    },
    *fetchUrl(_, {call, put}){
      const response = yield call(queryUrl);
      if(response.status === 'SUCCESS'){
        yield put({
          type: 'urlList',
          payload: response.data,
        });
      }
    },
    *fetchCurrent(_, { call, put }) {
      const response = yield call(queryCurrent);
      if(response.status === 'SUCCESS'){
        yield put({
          type: 'saveCurrentUser',
          payload: {
            name:response.data.name,
            userid:response.data.id,
            avatar:'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png',
            notifyCount: 0,
            unreadCount: 0,
          },
        });
      }
    },
  },

  reducers: {
    list(state, action) {
      return {
        ...state,
        list: action.payload.list,
        pagination:action.payload.pagination
      };
    },
    permission(state, action){
      return {
        ...state,
        permission:action.payload,
      };
    },
    role(state, action){
      return {
        ...state,
        role: action.payload,
      };
    },
    permissionFather(state, action){
      return {
        ...state,
        father: action.payload,
      };
    },
    updatePermission(state, action){
      return {
        ...state,
      }
    },
    addPermission(state, action){
      return {
        ...state,
      }
    },
    updateRole(state, action){
      let newRole = [];
      console.log(action);
      for(let i=0; i<state.role.length; i++){
        if(state.role[i].id === action.payload.id){
          newRole.push(action.payload);
        }else{
          newRole.push(state.role[i]);
        }
      }
      return {
        ...state,
        role:newRole
      }
    },
    deleteRole(state, action){
      let newRole = [];
      for(let i=0; i<state.role.length; i++){
        if(state.role[i].id !== action.payload){
          newRole.push(state.role[i]);
        }
      }
      return {
        ...state,
        role:newRole
      }
    },
    addRole(state, action){
      return {
        ...state,
        role:state.role.concat(action.payload)
      }
    },
    updateUser(state, action){
      let newList = [];
      for(let i=0; i<state.list.length; i++){
        if(state.list[i].id === action.payload.id){
          action.payload.created_at = state.list[i].created_at;
          newList.push(action.payload);
        }else{
          newList.push(state.list[i]);
        }
      }
      return {
        ...state,
        list:newList
      }
    },
    deleteUser(state, action){
      let newList = [];
      for(let i=0; i<state.list.length; i++){
        if(state.list[i].id !== action.payload){
          newList.push(state.list[i]);
        }
      }
      return {
        ...state,
        list:newList
      }
    },
    addUser(state, action){
      return {
        ...state,
        list:state.list.concat(action.payload)
      }
    },
    urlList(state, action){
      return {
        ...state,
        url: action.payload,
      };
    },
    save(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    saveCurrentUser(state, action) {
      return {
        ...state,
        currentUser: action.payload || {},
      };
    },
    changeNotifyCount(state, action) {
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
          notifyCount: action.payload.totalCount,
          unreadCount: action.payload.unreadCount,
        },
      };
    },
  },
};
